<?php
	namespace DB;
	
	use DB\Connection;
	use API\Api;
	
	class Table
	{
		protected $connection;
		
		protected $api;
		
		protected $table;
		
		protected $query;
		
		protected $column;
		
		protected $where;
		
		protected $orderby;
		
		protected $groupby;
		
		protected $having;
		
		public function __construct($table=null)
		{
			$api=new Api();
			$this->connection=new Connection();
			$this->connection->Open();
			$this->connection->ConnectDB();
			$this->table=$table;
		}
		
		public function setTable($table)
		{
			$this->table=$table;
		}
		
		public function getTable()
		{
			return $this->table;
		}

		public function select($type_data='assoc',$column='*',$condition=null,$orderby=null,$groupby=null,$having=null)
		{
			$data=null;
			$this->setColumn($column);
			$this->setWhere($condition);
			$this->setOrderBy($orderby);
			$query='SELECT '.$this->getColumn().' FROM '.$this->getTable().' WHERE '.$this->getWhere();
			$this->setQuery($query);
			if($type_data=='assoc')
			{
				$data=$this->getAssoc($this->getQuery());
			}
			else
			{
				
			}
			return $data;
		}
		
		public function getResult($sql='',$error='Truy vấn thất bại')
		{
			if(!empty($sql))
			{
				$rs=mysql_query($sql, $this->connection->getConnection()) or die($error);
			}
			else
			{
				$rs=mysql_query($this->getQuery(), $this->connection->getConnection()) or die($error);
			}
			return $rs;
		}
		
		public function getRecords($data)
		{
			$record=0;
			if(is_array($data))
			{
				$record=count($data);
			}
			else
			{
				$record=mysql_num_rows($data);
			}
			return $record;
		}
		/*
		* Phương thức trả về mảng dữ liệu theo kiểu chỉ
		*/
		public function getAssoc($sql,$error=null)
		{
			$data=null;
			$rs=$this->getResult($sql,$error);
			if($rs)
			{
				if($this->getRecords($rs) > 0)
				{
					while($d=mysql_fetch_assoc($rs))
					{
						$data[]=$d;
					}
				}
			}
			return $data;
		}
		
		public function setColumn($column)
		{
			if(is_array($column))
			{
				$this->column .= implode(',',$column);
			}
			else
			{
				$this->column='*';
			}
			$this->column=rtrim($this->column,',');
		}
		
		public function getColumn()
		{
			return $this->column;
		}
		
		public function setQuery($query)
		{
			$this->query=$query;
		}
		
		public function getQuery()
		{
			return $this->query;
		}
		
		public function setWhere($condition)
		{
			$this->where='1';
			if(is_array($condition))
			{
				foreach($condition as $k => $c)
				{
					$this->where .= ' AND '.$k.'="'.$c.'"';
				}
			}
		}
		
		public function getWhere()
		{
			return $this->where;
		}
		
		public function setOrderBy($orderby)
		{
			$this->orderby='';
			if(is_array($orderby))
			{
				foreach($orderby as $p => $o)
				{
					$this->orderby .= $p.' '.$o.',';
				}
			}
			$this->orderby=rtrim($this->orderby,',');
		}
		
		public function getOrderBy()
		{
			return $this->orderby;
		}
		
		public function setGroupBy($groupby)
		{
			$this->groupby='';
			if(is_array($orderby))
			{
				foreach($orderby as $g)
				{
					$this->groupby .= $g.',';
				}
			}
			$this->groupby=rtrim($this->groupby,',');
		}
		
		public function getGroupBy()
		{
			return $this->groupby;
		}
	}
?>